package com.association.main.controller;

import com.association.main.common.R;
import com.association.main.domain.Clubs;
import com.association.main.domain.Notifications;
import com.association.main.domain.Users;
import com.association.main.dto.NotificationsDto;
import com.association.main.service.ClubsService;
import com.association.main.service.NotificationsService;
import com.association.main.service.UsersService;
import com.association.main.utils.JwtUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/notifications")
public class NotificationsController {
    @Autowired
    private NotificationsService notificationsService;

    @Autowired
    private UsersService usersService;

    @Autowired
    private ClubsService clubsService;




    /*
    * 首页分页查询
    * */
    @GetMapping
    public R<Page> notificationsList(int page,int pageSize,Integer clubId,HttpServletRequest request){
        Page pageInfo=new Page(page,pageSize);
        LambdaQueryWrapper<Notifications> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(clubId!=null,Notifications::getClubId,clubId);
        List<Notifications> list = notificationsService.list(lambdaQueryWrapper);
        notificationsService.page(pageInfo,lambdaQueryWrapper);

        List<NotificationsDto> notificationsDtoList=list.stream().map((item)->{
            NotificationsDto notificationsDto = new NotificationsDto();
            BeanUtils.copyProperties(item,notificationsDto);
            Clubs clubs = clubsService.getById(item.getClubId());
            Users users = usersService.getById(item.getCreatedBy());
            notificationsDto.setUsers(users);
            notificationsDto.setClubs(clubs);
            return notificationsDto;
        }).collect(Collectors.toList());
        // 处理权限问题
        String token = request.getHeader("token");
        String userType = JwtUtil.getUserType(token);
        if ("member".equals(userType)) {
            Integer userId = JwtUtil.getUserId(token);
            LambdaQueryWrapper<Clubs> clubsWrapper = new LambdaQueryWrapper<>();
            clubsWrapper.eq(Clubs::getCreatedBy, userId);
            List<Clubs> clubsList = clubsService.list(clubsWrapper);
            notificationsDtoList = notificationsDtoList.stream().filter(m -> clubsList.stream().anyMatch(c -> m.getClubs().equals(c))).collect(Collectors.toList());
        }

        pageInfo.setRecords(notificationsDtoList);
        return R.success(pageInfo);

    }

    @PostMapping
    public R<Notifications> add(@RequestBody Notifications notifications, HttpServletRequest request){
        String token = request.getHeader("token");
        Integer userId = JwtUtil.getUserId(token);

        notifications.setUpdatedAt(LocalDateTime.now());
        notifications.setCreatedBy(userId);
        notifications.setCreatedAt(LocalDateTime.now());
        boolean save = notificationsService.save(notifications);
        return save == true ? R.success(notifications) : R.error("添加失败，未知错误");
    }

    @PutMapping
    public R<String> update(@RequestBody Notifications notifications){
        boolean update = notificationsService.updateById(notifications);
        return update == true ? R.success("修改成功") : R.error("修改失败，未知错误");
    }

    @DeleteMapping
    public R<String> delete(@RequestBody Notifications notifications){
        boolean delete = notificationsService.removeById(notifications.getId());
        return delete == true ? R.success("删除成功") : R.error("删除失败，未知错误");
    }


}
